import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ScanFile {

	private static PrintWriter implementsOut;
	private static PrintWriter classOut;
	private static PrintWriter extendsOut;
	private static String path = "E:/GitRepositories/cassandra/src/java/org"; //Cassandra source code path
	public static void main(String[] args) throws IOException {
		implementsOut = new PrintWriter(new FileWriter("implements.txt"));
		classOut = new PrintWriter(new FileWriter("class.txt"));
		extendsOut = new PrintWriter(new FileWriter("extends.txt"));
		File folder = new File(path);
		scanFolders(folder, "org");
		implementsOut.close();
		classOut.close();
		extendsOut.close();
	}
	
	public static void scanFolders(File folder, String pack) throws IOException {
		File[] subFolders = folder.listFiles();
		for (File f : subFolders) {
			if (f.isDirectory()) {
				scanFolders(f, pack + "." + f.getName());
			} else if (f.getName().endsWith(".java")){
				System.out.println("Find: " + f.getAbsolutePath());
				BufferedReader in = new BufferedReader(new FileReader(f.getAbsolutePath()));
				String line = in.readLine();
				Pattern cpat = Pattern.compile("^.* class [A-Z].*$");
				Pattern ipat = Pattern.compile("^.* interface [A-Z].*$"); 
				while (line != null) {
					line = line.replaceAll("<.*>", "");
					line = line.replace("}", "");
					line = line.replace('\t', ' ');
					line = line.replace("{", "");
					Matcher cm = cpat.matcher(line);
					Matcher im = ipat.matcher(line);
					if (cm.find()) {
						String cname = null;
						String parentC = null;
						String[] implI = null;
						String[] lineSegs = line.split(" ");
						for (int i = 0; i < lineSegs.length; i++) {
							if (lineSegs[i].equals("class")) {
								cname = lineSegs[i + 1];
								if (!(cname + ".java").equals(f.getName())) {
									cname = f.getName().split("\\.")[0] + "." + cname;
								}
							} else if (lineSegs[i].equals("extends")) {
								parentC = lineSegs[i + 1];
							} else if (lineSegs[i].equals("implements")) {
								implI = Arrays.copyOfRange(lineSegs, i + 1, lineSegs.length);
							}
						}
						classOut.println(cname + " 1 " + pack + " null " + " 0");
						if (parentC != null) {
							extendsOut.println(cname + "\t" + parentC);
						}
						if (implI != null) {
							for (int i = 0; i < implI.length; i++) {
								implementsOut.println(cname + "\t" + implI[i]);
							}
						}
						
					} else if (im.find()) {
						String cname = null;
						String parentC = null;
						String[] lineSegs = line.split(" ");
						for (int i = 0; i < lineSegs.length; i++) {
							if (lineSegs[i].equals("interface")) {
								cname = lineSegs[i + 1];
								if (!(cname + ".java").equals(f.getName())) {
									cname = f.getName().split("\\.")[0] + "." + cname;
								}
							}  else if (lineSegs[i].equals("extends")) {
								parentC = lineSegs[i + 1];
							}
						}
						classOut.println(cname + " 0 " + pack + " null " + " 0");
						if (parentC != null) {
							extendsOut.println(cname + "\t" + parentC);
						}
					}
					line = in.readLine();
				}
				in.close();
			}
		}
	}

}
